package handler

import (
	"gin-products/api/request"
	"gin-products/inter/dao"
	"gin-products/inter/model"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func AddProduct(c *gin.Context) {
	var req request.AddProductReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	var product model.Product
	dao.GetOneByFields(&model.Product{
		Id: req.Id,
		//Password:  req.Password,
		//Email:     req.Email,

	}, &product)
	if product.Id != 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "商品已存在",
			"data": nil,
		})
		return
	}
	product = model.Product{

		Title:    req.Title,
		Price:    req.Price,
		Num:      req.Num,
		Describe: req.Describe,
	}
	if !dao.Create(&product) {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "商品添加失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "商品添加成功",
		"data": product,
	})

}

func UpdateProduct(c *gin.Context) {
	var req request.UpdateProductReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	var product model.Product
	dao.GetOneByFields(&model.Product{
		Id: req.Id,
		//Password:  req.Password,
		//Email:     req.Email,

	}, &product)
	if product.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}
	product = model.Product{
		Id:       req.Id,
		Title:    req.Title,
		Price:    req.Price,
		Num:      req.Num,
		Describe: req.Describe,
	}
	if !dao.Update(&product) {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "商品修改失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "商品修改成功",
		"data": product,
	})

}

func DelProduct(c *gin.Context) {
	var req request.DelProductReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	var product model.Product
	dao.GetOneByFields(&model.Product{
		Id: req.Id,
		//Password:  req.Password,
		//Email:     req.Email,

	}, &product)
	if product.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}
	product = model.Product{
		Id: req.Id,
	}
	if !dao.Delete(&product) {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "商品删除失败",
			"data": false,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "商品删除成功",
		"data": true,
	})
}

func GetProduct(c *gin.Context) {
	var req request.GetProductReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "验证失败",
			"data": nil,
		})
		return
	}
	atoid, _ := strconv.Atoi(c.PostForm("id"))

	id := uint(atoid)
	var product model.Product
	dao.GetOneByFields(&model.Product{
		Id: req.Id,
		//Password:  req.Password,
		//Email:     req.Email,

	}, &product)
	if product.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "商品不存在",
			"data": nil,
		})
		return
	}
	if !dao.GetOneById(id, &product) {
		c.JSON(http.StatusOK, gin.H{
			"code": 1000,
			"msg":  "商品详情展示失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "商品详情展示成功",
		"data": product,
	})

}
